package com.jinlejia.bbs.adapter.message;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.CommentBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsAdapterAnswerItemBinding;
import com.jinlejia.bbs.databinding.BbsMp3ViewLayoutBinding;
import com.jinlejia.bbs.ui.mine.others.BBSOtherUserMainActivity;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.mp3.Mp3AdapterHelper;

import java.util.ArrayList;


/**
 * @author 王进 on 2019/3/11.
 */

public class AnswerAdapter extends Mp3AdapterHelper<ArrayList<CommentBean>> {


    public AnswerAdapter(Context context, AudioController audioController, RecyclerView recyclerView) {
        super(context, audioController, recyclerView);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case Inner.TYPE_ITEM:
                viewHolder = new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_answer_item, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return Inner.TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            bindItemViewHolder((ItemViewHolder) holder, position);
        }
    }

    @Override
    public int getItemCount() {
        if (mData == null || mData.size() == 0) {
            return 1;
        }
        return mData.size();
    }

    @Override
    protected SoundBean getMp3SoundBean(int position) {
        CommentBean bean = mData.get(position);
        InformationBean info = bean.getCommentedInfo();
        if (info==null){
            info = bean.getCommentInfo();
        }
        if (info!=null){
            return info.getSoundContent();
        }
        return null;
    }

    @Override
    protected BbsMp3ViewLayoutBinding getMp3Binding(int position) {
        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForLayoutPosition(position);
        BbsMp3ViewLayoutBinding mp3Binding = null;
        if (holder instanceof ItemViewHolder){
            BbsAdapterAnswerItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            if (binding.soundLayout.getVisibility()==View.VISIBLE){
                mp3Binding = binding.musicView.getBinding();
            }
        }
        return mp3Binding;
    }

    //region item
    private class ItemViewHolder extends RecyclerView.ViewHolder {

        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
    private void bindItemViewHolder(ItemViewHolder holder, int position) {
        final BbsAdapterAnswerItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final CommentBean bean = mData.get(position);
        binding.setBean(bean);
        int action = bean.getCommentAction();
        if (action == CommentBean.ACTION_COMMENT) {
            binding.actionTextView.setText("评论");
            binding.answerUserTextView.setVisibility(View.VISIBLE);
            binding.flagView.setVisibility(View.VISIBLE);
            binding.commentTextView.setVisibility(View.VISIBLE);
        } else if (action == CommentBean.ACTION_LIKE) {
            binding.actionTextView.setText("赞了这条内容");
            binding.answerUserTextView.setVisibility(View.GONE);
            binding.flagView.setVisibility(View.GONE);
            binding.commentTextView.setVisibility(View.GONE);
        } else if (action == CommentBean.ACTION_REPLY) {
            binding.actionTextView.setText("回答了这条内容");
            binding.answerUserTextView.setVisibility(View.GONE);
            binding.flagView.setVisibility(View.GONE);
            binding.commentTextView.setVisibility(View.GONE);
        }
        binding.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(bean.getTopicCode()))return;
                Intent intent = new Intent();
                intent.setClass(mContext, BBSReplyDetailActivity.class);
                TopicBean topic = new TopicBean();
                topic.setTopicCode(bean.getTopicCode());
                intent.putExtra("topic",topic);
                mContext.startActivity(intent);
            }
        });
        binding.userAvatarImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, BBSOtherUserMainActivity.class);
                BBSUserBean user = bean.getCommentUser();
                intent.putExtra("pin",user.getUserPin());
                mContext.startActivity(intent);
            }
        });
        binding.actionLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(bean.getTopicCode()))return;
                Intent intent = new Intent();
                intent.setClass(mContext, BBSReplyDetailActivity.class);
                TopicBean topic = new TopicBean();
                topic.setTopicCode(bean.getTopicCode());
                intent.putExtra("topic",topic);
                mContext.startActivity(intent);
            }
        });
        BBSUserBean user = bean.getCommentedUser();
        if (user==null){
            user = bean.getCommentUser();
        }
        InformationBean info = bean.getCommentedInfo();
        if (info==null){
            info = bean.getCommentInfo();
        }
        if (info.getSoundContent()==null){
            binding.soundLayout.setVisibility(View.GONE);
            binding.contentTextView.setVisibility(View.VISIBLE);
            String builder = "<font color='#4A93C3'>" +
                    user.getUserName() +
                    "</font>" +
                    "：" +
                    info.getMessageContent();
            Spanned html = Html.fromHtml(builder);
            binding.contentTextView.setText(html);
        }else {
            binding.contentTextView.setVisibility(View.GONE);
            binding.soundLayout.setVisibility(View.VISIBLE);
            final SoundBean content = info.getSoundContent();
            initMp3View(binding.musicView.getBinding(),content,position);
        }

    }
    //endregion

    private static class Inner {
        final static int TYPE_ITEM = 1;
    }


}
